/*
 * @Author: 蔡继周
 * @Date: 2021-11-13 14:43:14
 * @LastEditTime : 2021-12-16 14:47:37
 * @LastEditors  : Shaquille.Cai
 * @Description: I have to do something...
 * @FilePath     : \\vue3-admin-with-blog\\src\\store\\moduleInfo\\index.js
 */
import storage from '@/utils/storage'
import { getPermissionList } from '@/api/role'
import { login, getUserInfo } from '@/api/user'
import { FlatToTree } from "@/utils/utils";



const state = {
    userInfo: {},
    userStateList: [{
            value: 1,
            label: "在职",
        },
        {
            value: 2,
            label: "离职",
        },
        {
            value: 3,
            label: "试用期",
        },
    ],
    menuList: []
};
const getters = {
    userInfo: (state) => storage.getItem("userInfo") || state.userInfo,
    token: (state) => storage.getItem("token") || state.token || null,
    userStateList: (state) => state.userStateList,
    menuList: (state) => state.menuList,
};
const mutations = {
    LOGIN(state, token) {
        state.token = token;
        storage.setItem('token', token)
    },
    SAVE_USER_INFO(state, userInfo) {
        state.userInfo = userInfo;
        storage.setItem('userInfo', userInfo)
    },
    SAVE_MENU_RES(state, menuList) {
        state.menuList = menuList;
        storage.setItem('menuList', menuList)
    },
    saveActionList(state, actionList) {
        state.actionList = actionList;
        storage.setItem('actionList', actionList)
    },
    saveNoticeCount(state, noticeCount) {
        state.noticeCount = noticeCount;
        storage.setItem('noticeCount', noticeCount)
    }
}

const actions = {
    setMenuRes({ commit }) {
        return new Promise((resolve, reject) => {
            getPermissionList().then((res) => {
                const menuResult = [...res].filter((v) => v.menuType == 0);
                const actionResult = [...res].filter((v) => v.menuType == 1); //按钮权限
                const menuList = FlatToTree(menuResult, "parentId", "_id", "children");
                commit("SAVE_MENU_RES", menuList || []); //menu渲染数据
                // 路由渲染数据
                const asyncRouter = [...res].filter(v => v.component)
                    .map(v => {
                    return {
                        name: v.menuNameKey,
                        path: v.path,
                        meta: {
                            title: v.menuName
                        },
                        redirect: null,
                        component: v.component,
                        parentId: v.parentId,
                        _id: v._id
                    }
                    })
                resolve(asyncRouter)
            });
        })
    },
    login({ commit }, user) {
        return new Promise((resolve, reject) => {
            login(user).then((res) => {
                commit("LOGIN", res.token);
                resolve()
            }).catch(error => {
                reject(error)
            });
        })
    },
    getUserInfo({ commit }) {
        return new Promise((resolve, reject) => {
            getUserInfo().then((res) => {
                commit("SAVE_USER_INFO", res);
                resolve()
            }).catch(error => {
                reject(error)
            });
        })
    },
    // remove token
    resetToken({
        commit
    }) {
        return new Promise(resolve => {
            storage.clearAll()
            resolve()
        })
    }
};

export default {
    namespaced: true,
    state,
    getters,
    actions,
    mutations
}